
//
// 101206 - AH - Created.
// 101223a- AH - (Garage) (*) Removed debug printout.
// 101223b- AH - (Garage) (*) Added text area to enter optional note. 
// 101224a- AH - Changed so can also used to edit not only create, renamed from
//               CreateTreePanel, setTreeToEdit(Tree) and edit(), renamed
//               getCreated() to getTree().
// 101224b- AH - CreateEditTreePanel().
// 101225a- AH - Changed so edit now returns the empty string if no changes 
//               made.
// 101225b- AH - Fixed that fields not filled with Tree's properties when to
//               edit.
// 110523 - AH - (Garage) Got away comment input.
// 110606 - AH - (Garage) Changes related to that tree now has SoekratName.
//

package org.soekrat.gui.swing.forest;

import java.awt.BorderLayout;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.soekrat.engine.SoekratName;
import org.soekrat.engine.forest.Tree;

/**
 * <p>
 *   Panel for creating or editing a
 *   {@link Tree}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
@SuppressWarnings("serial")
public final class CreateEditTreePanel extends JPanel 
{
  private JTextField m_nameField=new JTextField(25);
  
  private final TreeCreator m_creator;
  
  private Tree m_tree=null;
  
  /**
   * <p>
   *   Creates panel for editing only.
   * </p>
   */
  public CreateEditTreePanel(){ this(null); }
  
  /**
   * <p>
   *   Creates panel capable for creating
   *   {@link Tree}.
   * </p>
   * @param creator Object responsible for creating 
   *                {@code Tree}. Can be {@code null}, then panel is for editing
   *                only.
   */
  public CreateEditTreePanel(TreeCreator creator)
  { 
    m_creator=creator;
    
    setLayout(new BorderLayout());
    
    Box center=new Box(BoxLayout.Y_AXIS);
    
    Box row=new Box(BoxLayout.X_AXIS);
    row.add(new JLabel("Name: "));
    row.add(m_nameField);
    center.add(row);
    
    add(center, BorderLayout.CENTER);
  }

  /**
   * <p>
   *   Created
   *   {@link Tree} from end user's input.
   * </p>
   * <p>
   *   Created 
   *   {@code Tree} can be fetched using
   *   {@link #getTree()}.
   * </p>
   * @param parent Parent to 
   *               {@code Tree} to create, if {@code null} creates root.
   * @return Error message to be shown to end user if could not create, 
   *               {@code null} if did.
   * @throws IllegalStateException If is for editing only.              
   * @see #getTree()
   */
  public String create(Tree parent)
  {
    if (m_creator==null)
    {
      throw new IllegalStateException("for editing only");
    }
    
    m_tree=null;
    
    String name=m_nameField.getText();
    name=(name==null) ? "" : name.trim();
    if (name==null)
    {
      return "Missing name";
    }
    
    m_tree=m_creator.create(name, parent);
    
    return null;
  }
  
  /**
   * <p>
   *   Sets
   *   {@link Tree} to edit.
   * </p>
   * @param tree {@code Tree} to edit.
   */
  public void setTreeToEdit(Tree tree)
  {
    if (tree==null)
    {
      throw new NullPointerException("tree");
    }
    
    m_tree=tree;
    
    SoekratName name=m_tree.getSoekratName();
    m_nameField.setText(name.toString());
  }
  
  /**
   * <p>
   *   Make changes to 
   *   {@link Tree} to edit.
   * </p>
   * @return {@code null} if changes made, the empty string if no changes made 
   *         or error message to be shown to end user.
   * @throws IllegalStateException If has no tree to edit.
   */
  public String edit()
  {
    if (m_tree==null)
    {
      throw new IllegalStateException("has no tree to edit");
    }
    
    String name=m_nameField.getText();
    name=(name==null) ? "" : name.trim();
    if (name==null)
    {
      return "Missing name";
    }
    
    String oldName=m_tree.getName();
    
    boolean edited=false;
    
    if (!name.equals(oldName))
    {
      m_tree.setName(name);
      edited=true;
    }
    
    return edited ? null : "";
  }
  
  /**
   * <p>
   *   Gets last created
   *   {@link Tree} or
   *   {@link Tree} to be edited..
   * </p>
   * @return {@link Tree}, {@code null} if none created or none are edited.
   */
  public Tree getTree(){ return m_tree; }
  
}
